﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraEditors.Controls;

namespace QuanLyKho.PhanQuyen
{
    public partial class QuanLyPhanQuyen : Form
    {
        public QuanLyPhanQuyen()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            var data = DataAll.SelectAll("DkChung_NhanVien", "MaNhanVien,TenNhanVien", "");
            luNhanVien.Properties.DataSource = data;
            luNhanVien.Properties.Columns.Add(new LookUpColumnInfo("TenNhanVien"));
            luNhanVien.Properties.Columns.Add(new LookUpColumnInfo("Username"));
            luNhanVien.Properties.DisplayMember = "TenNhanVien";
            luNhanVien.Properties.ValueMember = "Username";
            //luNhanVien.Properties.Columns["Username"].Visible = false;
            luNhanVien.Properties.NullText = string.Empty;
            luNhanVien.Properties.TextEditStyle = TextEditStyles.Standard;
            //luNhanVien.Properties.SearchMode = SearchMode.AutoComplete;
            luNhanVien.Properties.ShowHeader = false;

            var datacn = DataAll.SelectAll("ChucNang", "ID,TenChucNang", "");
            luChuNang.Properties.DataSource = datacn;
            luChuNang.Properties.Columns.Add(new LookUpColumnInfo("TenChucNang"));
            luChuNang.Properties.Columns.Add(new LookUpColumnInfo("ID"));
            luChuNang.Properties.DisplayMember = "TenChucNang";
            luChuNang.Properties.ValueMember = "ID";
            luChuNang.Properties.NullText = string.Empty;
            luChuNang.Properties.TextEditStyle = TextEditStyles.Standard;
            //luChuNang.Properties.SearchMode = SearchMode.AutoComplete;
            luChuNang.Properties.ShowHeader = false;

        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void SetQuyen()
        {
            DataAll.Update("PhanQuyen", "[Xem] = '" + cbXem.Checked + "',[Them] = '" + cbThem.Checked + "',[Sua] = '" + cbSua.Checked 
                + "',[Xoa] = '"+ cbXoa.Checked + "',[Prints] = '" + cbIn.Checked + "',[DuyetYeuCau] = '" + cbDuyetYC.Checked + "',[HuyYeuCau] = '" + cbHuyYeuCau.Checked 
                + "',[GuiPhieu] = '" + cbGuiPhieu.Checked + "',[XuatExcel] = '"+ cbXuatEX.Checked + "',[XemBaoCao] = '" + cbXemBaoCao.Checked + "',[XemTinNhan] = '" + cbXemTinNhan.Checked
                + "' WHERE [UserName] = N'" + luNhanVien.EditValue + "' AND [IDChucNang]=" + luChuNang.EditValue);
        }

        private void bntLuuLai_Click(object sender, EventArgs e)
        {
            SetQuyen();
            MessageBox.Show("Gán quyền thành công", "Message", MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void cbXem_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbThem_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbSua_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbXoa_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbIn_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbDuyetYC_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbHuyYeuCau_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbGuiPhieu_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbXuatEX_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbXemBaoCao_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }

        private void cbXemTinNhan_CheckedChanged(object sender, EventArgs e)
        {
            SetQuyen();
        }
    }
}
